package com.example.yyjc.controller;

import com.example.yyjc.domain.countryanalyze.GoodsTopTen;
import com.example.yyjc.domain.countryanalyze.ShopTopTen;
import com.example.yyjc.domain.industryanalyze.*;
import com.example.yyjc.domain.selectionanalyze.CountrySecondCatCase;
import com.example.yyjc.service.ImportService;
import com.example.yyjc.util.ResultBean;
import com.example.yyjc.util.excel.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName ImportController
 * @Description TODO  数据导入
 * @Author lvyongwei
 * @Date 2021-01-28 10:19
 **/
@RestController
@CrossOrigin
@RequestMapping("/import")
public class ImportController {

    @Autowired
    private ImportService importService;

    @Autowired
    private ExcelUtils excelUtils;

    /**
     * 导入全部国家及日期信息
     * @return
     */
    @PostMapping("/importCountry")
    public ResultBean importCountry(MultipartFile file){
        return importService.importCountry(file);
    }

    /**
     * 导入商品订单数量占比
     * @param file
     * @return
     */
    @PostMapping("/importOrderCountAnalysis")
    public ResultBean importOrderCountAnalysis(MultipartFile file){
        return importService.importOrderCountAnalysis(file);
    }

    /**
     * 导入商品价格区间信息占比
     * @param file
     * @return
     */
    @PostMapping("/importPriceCountAnalysis")
    public ResultBean importPriceCountAnalysis(MultipartFile file){
        return importService.importPriceCountAnalysis(file);
    }

    /**
     * 导入各个国家每个月份一级类目占比
     * @return
     */
    @PostMapping("/importMainTitleScale")
    public ResultBean importMainTitleScale(MultipartFile file){
        return importService.importMainTitleScale(file);
    }

    /**
     * 导入国家各个月份物流订单
     * @return
     */
    @PostMapping("/importLogisticsOrder")
    public ResultBean importLogisticsOrder(MultipartFile file){
        return importService.importLogisticsOrder(file);
    }

    /**
     * 导入国家某个月份top100店铺
     * @param file
     * @return
     */
    @PostMapping("/importShopTopTen")
    public ResultBean importShopTopTen(MultipartFile file){
        /**
         * 获取到导入信息
         */
        List<Object> shopTopTens = excelUtils.upload(file, ShopTopTen.class);
        if(StringUtils.isEmpty(shopTopTens) || shopTopTens.size() == 0){
            return ResultBean.error("请按照模板要求填写导入信息,再进行导入");
        }
        List<ShopTopTen> shopTopTenList = (List) shopTopTens;
        try {
            int count = importService.importShopTopTen(shopTopTenList);
            return ResultBean.success("恭喜您，数据已全部导入成功！共 "+ count +" 条数据");
        } catch (Exception e){
            e.printStackTrace();
            return ResultBean.error("导入失败,请重新查看是否符合要求");
        }
    }

    /**
     * 导入国家某个月份top100商品
     * @param file
     * @return
     */
    @PostMapping("/importGoodsTopTen")
    public ResultBean importGoodsTopTen(MultipartFile file){
        /**
         * 获取到导入信息
         */
        List<Object> goodsTopTens = excelUtils.upload(file, GoodsTopTen.class);
        if(StringUtils.isEmpty(goodsTopTens) || goodsTopTens.size() == 0){
            return ResultBean.error("请按照模板要求填写导入信息,再进行导入");
        }
        List<GoodsTopTen> goodsTopTenList = (List) goodsTopTens;
        try {
            int count = importService.importGoodsTopTen(goodsTopTenList);
            return ResultBean.success("恭喜您，数据已全部导入成功！共 "+ count +" 条数据");
        } catch (Exception e){
            e.printStackTrace();
            return ResultBean.error("导入失败,请重新查看是否符合要求");
        }
    }

    /**
     * 导入国家每个月份top1店铺名称
     * @return
     */
    @PostMapping("/importCountryTopOneShopName")
    public ResultBean importCountryTopOneShopName(MultipartFile file){
        return importService.importCountryTopOneShopName(file);
    }

    /**
     * 导入国家的top1店铺所有商品信息
     * @return
     */
    @PostMapping("/importCountryTopOneShopMsg")
    public ResultBean importCountryToponeShopMsg(MultipartFile file){
        return importService.importCountryToponeShopMsg(file);
    }

    /**
     * 导入所有一级类目的每月的销售额
     * @return
     */
    @PostMapping("/importFirstCatSale")
    public ResultBean importFirstCatSale(MultipartFile file){
        List<Object> firstCatSales = excelUtils.upload(file, FirstCatSale.class);
        if(StringUtils.isEmpty(firstCatSales) || firstCatSales.size() == 0){
            return ResultBean.error("请按照模板要求填写导入信息,再进行导入");
        }
        List<FirstCatSale> firstCatSaleList = (List) firstCatSales;
        try {
            int count = importService.importFirstCatSale(firstCatSaleList);
            return ResultBean.success("恭喜您，数据已全部导入成功！共 "+ count +" 条数据");
        } catch (Exception e){
            return ResultBean.error("导入失败,请重新查看是否符合要求");
        }
    }

    /**
     * 导入所有二级类目的每月销售额
     * @return
     */
    @PostMapping("/importSecondCatSale")
    public ResultBean importSecondCatSale(MultipartFile file){
        List<Object> secondCatSales = excelUtils.upload(file, SecondCatSale.class);
        if(StringUtils.isEmpty(secondCatSales) || secondCatSales.size() == 0){
            return ResultBean.error("请按照模板要求填写导入信息,再进行导入");
        }
        List<SecondCatSale> secondCatSaleList = (List) secondCatSales;
        try {
            int count = importService.importSecondCatSale(secondCatSaleList);
            return ResultBean.success("恭喜您，数据已全部导入成功！共 "+ count +" 条数据");
        } catch (Exception e){
            return ResultBean.error("导入失败,请重新查看是否符合要求");
        }
    }

    /**
     * 导入二级类目各个月份各个国家销售额
     * @return
     */
    @PostMapping("/importSecondCatCountrySaleCount")
    public ResultBean importSecondCatCountrySaleCount(MultipartFile file){
        List<Object> secondCatCountrySaleCounts = excelUtils.upload(file, SecondCatCountrySaleCount.class);
        if(StringUtils.isEmpty(secondCatCountrySaleCounts) || secondCatCountrySaleCounts.size() == 0){
            return ResultBean.error("请按照模板要求填写导入信息,再进行导入");
        }
        List<SecondCatCountrySaleCount> secondCatCountrySaleCountList = (List) secondCatCountrySaleCounts;
        try {
            int count = importService.importSecondCatCountrySaleCount(secondCatCountrySaleCountList);
            return ResultBean.success("恭喜您，数据已全部导入成功！共 "+ count +" 条数据");
        } catch (Exception e){
            return ResultBean.error("导入失败,请重新查看是否符合要求");
        }
    }

    /**
     * 导入二级类目每个月份国家物流订单量
     * @return
     */
    @PostMapping("/importDeliveryCountry")
    public ResultBean importDeliveryCountry(MultipartFile file){
        List<Object> deliveryCountrys = excelUtils.upload(file, DeliveryCountry.class);
        if(StringUtils.isEmpty(deliveryCountrys) || deliveryCountrys.size() == 0){
            return ResultBean.error("请按照模板要求填写导入信息,再进行导入");
        }
        List<DeliveryCountry> deliveryCountries = (List) deliveryCountrys;
        try {
            int count = importService.importDeliveryCountry(deliveryCountries);
            return ResultBean.success("恭喜您，数据已全部导入成功！共 "+ count +" 条数据");
        } catch (Exception e){
            return ResultBean.error("导入失败,请重新查看是否符合要求");
        }
    }

    /**
     * 导入二级类目每个月份价格区间商品/订单数量
     * @return
     */
    @PostMapping("/importSecondCatPriceScale")
    public ResultBean importSecondCatPriceScale(MultipartFile file){
        return importService.importSecondCatPriceScale(file);
    }

    /**
     * 导入二级类目每个月份订单区间商品/订单数量
     * @return
     */
    @PostMapping("/importSecondCatOrderScale")
    public ResultBean importSecondCatOrderScale(MultipartFile file){
        return importService.importSecondCatOrderScale(file);
    }

    /**
     * 导入二级类目每个月top10店铺信息
     * @return
     */
    @PostMapping("/importSecondCatShopTopTen")
    public ResultBean importSecondCatShopTopTen(MultipartFile file){
        List<Object> secondCatShopTopTens = excelUtils.upload(file, SecondCatShopTopTen.class);
        if(StringUtils.isEmpty(secondCatShopTopTens) || secondCatShopTopTens.size() == 0){
            return ResultBean.error("请按照模板要求填写导入信息,再进行导入");
        }
        List<SecondCatShopTopTen> secondCatShopTopTenList = (List) secondCatShopTopTens;
        try {
            int count = importService.importSecondCatShopTopTen(secondCatShopTopTenList);
            return ResultBean.success("恭喜您，数据已全部导入成功！共 "+ count +" 条数据");
        } catch (Exception e){
            return ResultBean.error("导入失败,请重新查看是否符合要求");
        }
    }

    /**
     * 导入二级类目每个月top100商品信息
     * @return
     */
    @PostMapping("/importSecondCatGoodsTopTen")
    public ResultBean importSecondCatGoodsTopTen(MultipartFile file){
        List<Object> secondCatGoodsTopTens = excelUtils.upload(file, SecondCatGoodsTopTen.class);
        if(StringUtils.isEmpty(secondCatGoodsTopTens) || secondCatGoodsTopTens.size() == 0){
            return ResultBean.error("请按照模板要求填写导入信息,再进行导入");
        }
        List<SecondCatGoodsTopTen> secondCatGoodsTopTenList = (List) secondCatGoodsTopTens;
        try {
            int count = importService.importSecondCatGoodsTopTen(secondCatGoodsTopTenList);
            return ResultBean.success("恭喜您，数据已全部导入成功！共 "+ count +" 条数据");
        } catch (Exception e){
            return ResultBean.error("导入失败,请重新查看是否符合要求");
        }
    }

    /**
     * 导入二级类目每个月top10国家top3物流信息
     * @return
     */
    @PostMapping("/importSecondCatTopLogistics")
    public ResultBean importSecondCatTopLogistics(MultipartFile file){
        List<Object> secondCatTopLogistics = excelUtils.upload(file, SecondCatTopLogistics.class);
        if(StringUtils.isEmpty(secondCatTopLogistics) || secondCatTopLogistics.size() == 0){
            return ResultBean.error("请按照模板要求填写导入信息,再进行导入");
        }
        List<SecondCatTopLogistics> secondCatTopLogisticsList = (List) secondCatTopLogistics;
        try {
            int count = importService.importSecondCatTopLogistics(secondCatTopLogisticsList);
            return ResultBean.success("恭喜您，数据已全部导入成功！共 "+ count +" 条数据");
        } catch (Exception e){
            return ResultBean.error("导入失败,请重新查看是否符合要求");
        }
    }

    /**
     * 导入Aliexpress前10国家信息
     * @return
     */
    @PostMapping("/importAliexpress")
    public ResultBean importAliexpress(MultipartFile file){
        List<Object> aliexpresses = excelUtils.upload(file, Aliexpress.class);
        if(StringUtils.isEmpty(aliexpresses) || aliexpresses.size() == 0){
            return ResultBean.error("请按照模板要求填写导入信息,再进行导入");
        }
        List<Aliexpress> aliexpressList = (List) aliexpresses;
        try {
            int count = importService.importAliexpress(aliexpressList);
            return ResultBean.success("恭喜您，数据已全部导入成功！共 "+ count +" 条数据");
        } catch (Exception e){
            return ResultBean.error("导入失败,请重新查看是否符合要求");
        }
    }

    /**
     * 导入Aliexpress的前10二级类目信息
     * @return
     */
    @PostMapping("/importAliexpressSecondCatTopTen")
    public ResultBean importAliexpressSecondCatTopTen(MultipartFile file){
        List<Object> aliexpressSecondCatTopTens = excelUtils.upload(file, AliexpressSecondCatTopTen.class);
        if(StringUtils.isEmpty(aliexpressSecondCatTopTens) || aliexpressSecondCatTopTens.size() == 0){
            return ResultBean.error("请按照模板要求填写导入信息,再进行导入");
        }
        List<AliexpressSecondCatTopTen> aliexpressSecondCatTopTenList = (List) aliexpressSecondCatTopTens;
        try {
            int count = importService.importAliexpressSecondCatTopTen(aliexpressSecondCatTopTenList);
            return ResultBean.success("恭喜您，数据已全部导入成功！共 "+ count +" 条数据");
        } catch (Exception e){
            return ResultBean.error("导入失败,请重新查看是否符合要求");
        }
    }

    /**
     * 导入国家每个月份二级类目情况
     * @return
     */
    @PostMapping("/importCountrySecondCatCase")
    public ResultBean importCountrySecondCatCase(MultipartFile file){
        List<Object> countrySecondCatCases = excelUtils.upload(file, CountrySecondCatCase.class);
        if(StringUtils.isEmpty(countrySecondCatCases) || countrySecondCatCases.size() == 0){
            return ResultBean.error("请按照模板要求填写导入信息,再进行导入");
        }
        List<CountrySecondCatCase> countrySecondCatCaseList = (List) countrySecondCatCases;
        try {
            int count = importService.importCountrySecondCatCase(countrySecondCatCaseList);
            return ResultBean.success("恭喜您，数据已全部导入成功！共 "+ count +" 条数据");
        } catch (Exception e){
            return ResultBean.error("导入失败,请重新查看是否符合要求");
        }
    }
}